package com.marketsim.dao;

import com.marketsim.common.bean.OrderBean;
import com.marketsim.order.OrderType;

import java.io.*;
import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Alex on 28.03.2014.
 */
public class OrdersDAO {

    public List<OrderBean> getOrdersFromFile(File file) {
        List<OrderBean> result = new LinkedList<>();
        try {
            InputStream inputStream = new FileInputStream(file);
            BufferedReader in = new BufferedReader(new InputStreamReader(inputStream));
            String inputLine;
            while ((inputLine = in.readLine()) != null) {
                String[] ordersInfo = inputLine.split(",");
                OrderBean orderBean = new OrderBean();
                int year = Integer.valueOf(ordersInfo[0]);
                int month = Integer.valueOf(ordersInfo[1]);
                int day = Integer.valueOf(ordersInfo[2]);
                Calendar cal = Calendar.getInstance();
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, month - 1);
                cal.set(Calendar.DAY_OF_MONTH, day);
                cal.set(Calendar.HOUR_OF_DAY, 0);
                cal.set(Calendar.MINUTE, 0);
                cal.set(Calendar.SECOND, 0);
                cal.set(Calendar.MILLISECOND, 0);
                orderBean.setDate(cal.getTime());
                orderBean.setTicker(ordersInfo[3]);
                orderBean.setOrderType((ordersInfo[4].equals("Buy") ? OrderType.BUY_MARKET : OrderType.SELL_MARKET));
                orderBean.setAmount(Long.valueOf(ordersInfo[5]));
                result.add(orderBean);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }
}
